package com.atme.inform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aliyuncs.exceptions.ClientException;
import com.atme.inform.service.SmsService;
import com.atme.inform.util.SmsUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KafkaServiceImpl implements SmsService {


    /**
     * 短信验证
     * 基于注解监听多个topic，消费topic中消息
     * （注意：如果监听的topic不存在则会自动创建）
     */
    @KafkaListener(topics = {"phoneTopic"})
    public void phoneTopic(String message) throws ClientException {
        System.out.println("Service消费："+message);

        //获取kafka传过来的参数   json字符串
        JSONObject jsonObj = new JSONObject(message);
        String phoneMember=jsonObj.get("phone").toString();
        String yzm=jsonObj.get("yzm").toString();
        System.out.println(phoneMember);
        System.out.println(yzm);
        SmsUtil.sendSms(phoneMember,yzm);
    }

    /**
     * 发送生日短信
     * @param message
     */
    @KafkaListener(topics = "BirthdatePhoneTopic")
    public void BirthdatePhoneTopic(String message){
        System.out.println("Birthdate消费："+message);

        List<String> strings= JSON.parseObject(message,new TypeReference<List<String>>(){});
        for (String phone:strings){

            System.out.println(phone);
            //获取到每一个手机号，发送短信（未写）
        }

    }

    /**
     * 发送邮件
     * @param message
     */
    @KafkaListener(topics = "MailTopic")
    public void MailTopic(String message){
        System.out.println("MailTopic消费："+message);
        //调用mailService.sendMail()方法发送邮件
    }





}
